<template>
  <el-card>
    <div slot="header" class="header">
      <span>订单管理</span>
    </div>
    <!-- 查询表单 -->
    <el-form inline size="mini" :model="formData">
      <!-- 订单号 -->
      <el-form-item label="订单号">
        <el-input v-model="formData.orderNo"></el-input>
      </el-form-item>
      <!-- 收货人 -->
      <el-form-item label="收货人">
        <el-input v-model="formData.consignee"></el-input>
      </el-form-item>
      <!-- 手机号 -->
      <el-form-item label="手机号">
        <el-input v-model="formData.phone"></el-input>
      </el-form-item>
      <!-- 订单状态 -->
      <el-form-item label="订单状态">
        <el-select v-model="formData.orderState">
          <el-option value="已受理" label="已受理">已受理</el-option>
          <el-option value="派送中" label="派送中">派送中</el-option>
          <el-option value="已完成" label="已完成">已完成</el-option>
        </el-select>
      </el-form-item>
      <!-- 选择时间 -->
      <el-form-item label="选择时间">
        <el-date-picker
          v-model="formData.date"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <!-- 操作 -->
      <el-form-item>
        <el-button @click="search" type="primary">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table :data="tableData" :style="{width: w + 'px'}">
      <!-- 订单号 -->
      <el-table-column fixed prop="orderNo" label="订单号"></el-table-column>
      <!-- 下单时间 -->
      <el-table-column label="下单时间">
        <template slot-scope="scope">
          {{ scope.row.orderTime | formatHms }}
        </template>
      </el-table-column>
      <!-- 手机号 -->
      <el-table-column prop="phone" label="手机号"></el-table-column>
      <!-- 收货人 -->
      <el-table-column prop="consignee" label="收货人"></el-table-column>
      <!-- 配速地址 -->
      <el-table-column prop="deliverAddress" label="配速地址"></el-table-column>
      <!-- 送达时间 -->
      <el-table-column label="送达时间">
        <template slot-scope="scope">
          {{ scope.row.deliveryTime | formatHms }}
        </template>
      </el-table-column>
      <!-- 用户备注 -->
      <el-table-column prop="remarks" label="用户备注"></el-table-column>
      <!-- 订单金额 -->
      <el-table-column prop="orderAmount" label="订单金额"></el-table-column>
      <!-- 订单状态 -->
      <el-table-column prop="orderState" label="订单状态"></el-table-column>
      <!-- 操作 -->
      <el-table-column width="90px" fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button
            @click="
              $router.push({
                path: '/order/order-detail',
                query: {id: scope.row.id},
              })
            "
            type="text"
            size="small"
          >
            查看
          </el-button>
          <el-button
            type="text"
            size="small"
            @click="$router.push(`/order/order-edit/${scope.row.id}`)"
          >
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      background
      class="mt20"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-size="pageSize"
      layout="total, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </el-card>
</template>

<script>
//解构ajax
import {getOrderListReq} from '@/api/order'
// 引入过滤器函数
// import {formatYMDhms} from '@/filters'
// 引入mixins 宽
import resizeTable from '@/mixins/resizeTable'

export default {
  mixins: [resizeTable],
  data() {
    return {
      //查询表单数据
      formData: {
        orderNo: '', //订单号
        consignee: '', //收货人
        phone: '', //手机号
        orderState: '', //订单状态
        date: [], //日期
      },
      //查询表单数据
      searchForm: {},

      //表格数据
      tableData: [],
      //分页数据
      currentPage: 1,
      pageSize: 3,
      total: 0,
      w: 0, //table宽度
    }
  },
  methods: {
    async getData() {
      let currentPage = this.currentPage
      let pageSize = this.pageSize
      //处理日期格式 处理默认值null问题
      let date = JSON.stringify(this.searchForm.date || '')
      //发送请求
      let res = await getOrderListReq({
        currentPage,
        pageSize,
        ...this.searchForm, //查询表单数据进来
        date,
      })
      //解构数据
      let {total, data} = res.data
      //数据赋值
      this.total = total
      this.tableData = data
    },
    //查询
    search() {
      //把当前页重置为1
      this.currentPage = 1

      //把当前formData的值 拷贝给查询表单使用
      this.searchForm = {...this.formData}

      this.getData()
    },
    //改变当前页码
    handleCurrentChange(val) {
      this.currentPage = val
      this.getData()
    },
  },
  created() {
    this.getData()
  },
}
</script>

<style lang="scss" scoped></style>
